#!/usr/bin/env  python
#coding:utf-8
import paramiko
import os
import sys

def fix_host(ip,pwd,pub):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        print ip,pwd
        try:
                ssh.connect(ip, username='root', password=pwd)
#               transport = ssh.get_transport()
#               transport.set_keepalive(30)
    	paramiko.util.log_to_file("filename.log")
                stdin, stdout, stderr = ssh.exec_command('echo %s >> ~/.ssh/authorized_keys'% pub )
                print stdout.readlines()
        except:
                ssh.close()
                return
        try:
                print os.system("ssh root@%s uptime" % ip)
        except:
                return


pub_str = file('/mnt/id_rsa.pub','rt').read()

with open('ip','r+') as f:
    line = f.readlines()

for x in line:
    fix_host(x.split()[0],x.split()[1],pub_str)